package com.cap.dreamcircle.View.Adapter.Item;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Common.RecyclerView.BaseItem;
import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Action.ActionSheetAction;
import com.cap.dreamcircle.Model.Action.LikeDreamCircleAction;
import com.cap.dreamcircle.Model.Bean.FriendsDreamEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.DisplayUtil;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.PreferenceUtils;
import com.cap.dreamcircle.Utils.SimpleMediaPlayerUtils;
import com.cap.dreamcircle.Utils.TimeUtils;
import com.cap.dreamcircle.View.DreamDetailActivity;
import com.cap.dreamcircle.View.PersonalInfoActivity;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by jihongwen on 16/6/30.
 */

public class SimpleFriendDreamItem extends BaseItem<FriendsDreamEntity> implements View.OnClickListener {

    FriendsDreamEntity resultsBean;
    private CircleImageView imv_dream_friend_header;
    private TextView tv_dream_friend_name;
    private TextView tv_dream_content;
    private TextView tv_friend_dream_time;
    private ImageView imv_dream_like;
    private ImageView imv_dream_comment;
    private TextView tv_like_num;
    private TextView tv_comment_num;
    private RelativeLayout layout_audio;
    private ImageView imv_playing_status_audio;
    private ImageView imv_etc_menu;


    public SimpleFriendDreamItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_dream_circle;
    }

    @Override
    public void onCreateView(View rootView) {
        imv_dream_friend_header = (CircleImageView) rootView.findViewById(R.id.imv_dream_friend_header);
        tv_dream_friend_name = (TextView) rootView.findViewById(R.id.tv_dream_friend_name);
        tv_dream_content = (TextView) rootView.findViewById(R.id.tv_dream_content);
        tv_friend_dream_time = (TextView) rootView.findViewById(R.id.tv_friend_dream_time);
        imv_dream_like = (ImageView) rootView.findViewById(R.id.imv_dream_like);
        imv_dream_comment = (ImageView) rootView.findViewById(R.id.imv_dream_comment);
        tv_like_num=(TextView)rootView.findViewById(R.id.tv_like_num);
        tv_comment_num=(TextView)rootView.findViewById(R.id.tv_comment_num);
        layout_audio=(RelativeLayout)rootView.findViewById(R.id.layout_audio);
        imv_playing_status_audio=(ImageView)rootView.findViewById(R.id.imv_playing_status_audio);
        imv_etc_menu=(ImageView)rootView.findViewById(R.id.imv_etc_menu);
    }

    @Override
    public void onBindView(final FriendsDreamEntity response) {
        this.resultsBean = response;
        GlideUtils.getInstance().LoadContextBitmap(getContext(), Constants.BASE_URL + resultsBean.getHeadImgUrl(), imv_dream_friend_header, R.mipmap.default_header, R.mipmap.default_header, GlideUtils.LOAD_BITMAP);
        imv_dream_friend_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if((!response.isAnonymousUser())||(response.getDreamUserId().equals(PreferenceUtils.GetUserId()))){
                    Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
                    intent.putExtra("userId",resultsBean.getDreamUserId());
                    ActivityCompat.startActivity((Activity) getContext(), intent, null);
                }
            }
        });
        tv_dream_friend_name.setText(resultsBean.getNickName());
        tv_dream_content.setText(this.resultsBean.getDreamText());
        tv_friend_dream_time.setText(TimeUtils.formatTime(this.resultsBean.getCreateTime()));
//        tv_friend_dream_time.setText(this.resultsBean.getCreateTime());
        if (PreferenceUtils.GetUserId().equals(this.resultsBean.getDreamUserId())) {
            //自己的条目，actionsheet菜单中有删除、转发按钮
            imv_etc_menu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActionSheetAction action=new ActionSheetAction();
                    action.setEntity(resultsBean);
                    action.setPosition(getPosition());
                    EventBus.getDefault().post(action);
                }
            });
        } else {
            //别人的条目，actionsheet菜单中有举报按钮
            imv_etc_menu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActionSheetAction action=new ActionSheetAction();
                    action.setEntity(resultsBean);
                    action.setPosition(getPosition());
                    EventBus.getDefault().post(action);
                }
            });
        }
        if (this.resultsBean.isLiked()) {
            imv_dream_like.setEnabled(false);
            imv_dream_like.setImageResource(R.mipmap.imv_dream_liked);
        } else {
            imv_dream_like.setEnabled(true);
            imv_dream_like.setImageResource(R.mipmap.imv_dream_like);
            imv_dream_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyCounterUtils.SendKeyCounter("95328a9bc42748ae97066378c90c973e");
                    LikeDreamCircleAction action=new LikeDreamCircleAction();
                    action.setPosition(getPosition());
                    EventBus.getDefault().post(action);
                }
            });
        }
        if (this.resultsBean.getLikeCount() == 0) {
            tv_like_num.setVisibility(View.GONE);
        } else {
            tv_like_num.setText(this.resultsBean.getLikeCount()+"");
            tv_like_num.setVisibility(View.VISIBLE);
        }
        if(this.resultsBean.getCommentCount() == 0){
            tv_comment_num.setVisibility(View.GONE);
        }
        else {
            tv_comment_num.setVisibility(View.VISIBLE);
            tv_comment_num.setText(this.resultsBean.getCommentCount()+"");
        }
        if(!TextUtils.isEmpty(resultsBean.getDreamVoiceUrl())){
            KeyCounterUtils.SendKeyCounter("01f8b860b8fc4bb49c3dda3ff0b1ecf4");
            layout_audio.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layout_audio.getLayoutParams());
            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            int min_width = DisplayUtil.dip2px(App.GetInstance(), 128);
            int max_width = DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(App.GetInstance(), 36);
            int width = (DisplayUtil.GetDisplayWidth()-DisplayUtil.dip2px(getContext(),30))/3;
            layout_audio.getLayoutParams().width = width;
            layout_audio.requestLayout();
            imv_playing_status_audio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SimpleMediaPlayerUtils.getInstance(getContext()).play(Constants.BASE_URL + resultsBean.getDreamVoiceUrl(), new SimpleMediaPlayerUtils.VoicePlayInterface() {
                        @Override
                        public void onStartPlaying() {
                            imv_playing_status_audio.setImageResource(0);
                            imv_playing_status_audio.setImageDrawable(null);
                            imv_playing_status_audio.setImageURI(null);
                            imv_playing_status_audio.setImageResource(R.mipmap.audio_playing);
                        }

                        @Override
                        public void onPause() {
                            imv_playing_status_audio.setImageResource(0);
                            imv_playing_status_audio.setImageDrawable(null);
                            imv_playing_status_audio.setImageURI(null);
                            imv_playing_status_audio.setImageResource(R.mipmap.audio_pause);
                        }

                        @Override
                        public void onPlayCompleted() {
                            imv_playing_status_audio.setImageResource(0);
                            imv_playing_status_audio.setImageDrawable(null);
                            imv_playing_status_audio.setImageURI(null);
                            imv_playing_status_audio.setImageResource(R.mipmap.audio_pause);
                        }
                    });
                }
            });
        }
        else{
            layout_audio.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        KeyCounterUtils.SendKeyCounter("cf11e14d98e54682880a9a5f3143a4d9");
        Intent intent = new Intent(getContext(), DreamDetailActivity.class);
        intent.putExtra("dreamId",resultsBean.getDreamId()+"");
        intent.putExtra("voiceUrl",resultsBean.getDreamVoiceUrl());
        getContext().startActivity(intent);
    }
}
